package com.itqf.controller;


import com.itqf.entity.Address;
import com.itqf.entity.User;
import com.itqf.service.AddressService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AddressController", value = "/address")
public class AddressController extends BaseServlet {
    //初始化业务逻辑对象
    private static AddressService addressService = new AddressServiceImpl();

    /**
     * 展示地址
     *
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取当前用户的ID
        User user = (User) request.getSession().getAttribute("loginUser");

        if (user == null) {
            //自动登录失效
            //存入登录失败信息，返回给前端页面进行展示
            request.getSession().setAttribute("mag", "需要先登录！！！");
            //重新登录(重定向)
            return Constants.REDIRECT + "/login.jsp";
        }
        int uid = user.getUid();

        //调用业务逻辑，查询当前用户的地址信息
        List<Address> address = addressService.findAddressByUid(uid);

        //将地址信息存入request的作用域中
        request.setAttribute("addresslist",address);

        //响应（转发至地址展示页面）
        return Constants.FORWARD + "/self_info.jsp";
    }

    /**
     * 添加地址
     * @param request
     * @param response
     * @return
     * @throws SQLException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String add(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //1.获取前端发送的请求参数
        Map<String, String[]> map = request.getParameterMap();

        Address address = new Address();

        //将请求数据集合中的相关参数映射到实体类对象中
        BeanUtils.populate(address,map);

        //2.调用业务逻辑，进行添加地址
        addressService.saveAddress(address);
        //3.响应（转发至地址展示页面）
        return Constants.FORWARD + "/address?method=show" ;
    }


    public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取地址的id
        String aid = request.getParameter("aid");

        //2.调用业务逻辑，根据地址id进行删除
        addressService.deleteAddressByAid(aid);
        //3.响应（转发至地址展示页面）
        return Constants.FORWARD + "/address?method=show" ;
    }

    public String setDefultt(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //1.获取地址的id
        String aid = request.getParameter("aid");
        //获取当前用户ID
        User user = (User)request.getSession().getAttribute("loginUser");
        int uid = user.getUid();

        //2.调用业务逻辑，根据地址id进行删除
        addressService.setAddressToDefult(aid,uid);
        //3.响应（转发至地址展示页面）
        return Constants.FORWARD + "/address?method=show" ;
    }

    public String update(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //1.获取前端提交的参数
        Map<String, String[]> map = request.getParameterMap();
        //将数据集合中的相关数据映射到实体类对象中
        Address address = new Address();
        BeanUtils.populate(address,map);
        //2.调用业务逻辑，进行地址信息的修改
        addressService.updateAddressByAid(address);

        //3.响应（转发至地址展示页面）
        return Constants.FORWARD + "/address?method=show" ;
    }


}


